home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15888 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.9 KB

  1. Path: news.nask.org.pl!usenet
  2. From: piotrpar@blue.maloka.waw.pl (Piotr Parlewicz)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: 64K segment limit:Newbie seeks enlightenment
  5. Date: Mon, 08 Apr 1996 18:19:21 GMT
  6. Organization: Research and Academic Computer Network
  7. Message-ID: <4kbl3g$8si@bilbo.nask.org.pl>
  8. References: <Pine.ULT.3.91.960407231001.3447A-100000@essex.UCHSC.edu>
  9. NNTP-Posting-Host: s111.maloka.waw.pl
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. David McClure <mcclured@essex.UCHSC.edu> wrote:
  13.  
  14. >We're coding a computer sim in BC4.52 of human anatomy for and are
  15. >concerned about the 64K segment limit with 3D arrays 
  16. >that are upto several megabytes in size.
  17.  
  18. >Could anyone expound on the 64K segment limit in win3.1. My limited 
  19. >understanding of it is 64K = segment length = local heap size+ stack heap +
  20. >static data + 16 byte header.  Is this why arrays are limited to < 64K?
  21. >We want to use upto 4 or 8 meg total 3D arrays (they simulate human anatomy).
  22. >Right now we use triple pointers.  But how is this large array data 
  23. >accessed when the limit is 64K ? Also what is the limit for win3.51? 
  24. >I don't think we can use the huge memory model because that's only for 
  25. >DOS apps.
  26.  
  27.  
  28. >*****************************
  29. >*  David.McClure@UCHSC.edu  *
  30. >*****************************
  31.  
  32.  
  33. Hi David,
  34.  here are some pointers I thought you could use that I learned
  35. while programming for large arrays in BC++3.1. All the following 
  36. is true only for Windows 3.1 (16-bit protected mode). 
  37.  
  38. I DONT know if this is true in native Windows NT (3.51).
  39. However since all 16-bit applications should run in NT and Win95
  40. then I guess it should work when used in a 16-bit app.
  41.  
  42. When coding for NT, you should have no problems with large memory,
  43. since its supposed to be a true 32 bit system.
  44.  
  45. 1. You can use huge pointers in the large model under Windows 3.1
  46. The mechanism used by Windows in GlobalAlloc for large blocks of
  47. memory gives you 'tiled selectors'.
  48. The code generated for huge pointer arithmetic (+,-) handles the
  49. tiled selectors correctly.
  50. A 32 bit pointer in protected mode is made up of the selector (upper
  51. 16) bits and the offset (lower 16 bits)
  52.  
  53. A selector is a 16-bit id that maps into a table used by the CPU
  54. to calculate effective addresses.
  55.  
  56. There can be a maximum of around 8000 unique selectors.
  57. Underneath the 64KB selector-segments is linear memory space.
  58. When allocating more than 64KB of memory, Windows internally allocates
  59. consecutive selectors for each block of 64KB making up the requested
  60. big block in linear memory space.
  61. Unfortunately consecutive in this context means sel(n) = sel(n-1) + 8
  62. (huge pointer arithmetic handles this correctly)
  63.  
  64. However this is not all there is to it!
  65. When you have a pointer to a struct that straddles a segment boundary,
  66. (and this will be possible whenever your struct size is not a multiple
  67. of 2) and you try to refference a field in it that is after the
  68. segment boundary, the code generated to access the structs fields will
  69.  
  70. loop around and you will get incorrect values.
  71.  
  72. To get around this problem I suggest one of the following:
  73.  
  74. a) allways use padded structs to make sure their sizeof is a multiple
  75. of 2
  76.  
  77. b) Access the fields using your own little address fixer function
  78. that will do some hanky panky on the pointer. (This require a bit more
  79. explaining so if you want more on this e-mail me.)
  80.  
  81.  
  82. 2. Windows toolhelp.dll delivers some functions for reading and
  83. writing to GlobalAlloced memory.
  84.  
  85. 3. Remember that hmemcpy, is not hmemmov. You should not use it when
  86. the source and destionation memory blocks overlap.
  87. If you want I can e-mail you my own implementation of hmemmov in C.
  88.  
  89. 4. If you want to use C style 3-d arrays, I'm not sure how the huge
  90. keyword will influence your access. (I've never had to solve that
  91. problem.) Maybe you would be better of using a macro that converts the
  92. 3 coordinates into a single coordinate item offset in the big array.
  93.  
  94. I hope this is usefull to you.
  95.  
  96. Piotr Parlewicz
  97.  
  98. piotrpar@blue.maloka.waw.pl
  99.  
  100.  
  101.  
  102.